<template lang="html">
  <div class="define" v-if="goodsData">
    <ComHeader title="确认订单" leftArrow="true"/>
    <div class="address">
      <div class="has-address" @click="fnAddress()" v-if="addressData">
        <div class="info">
          <h4>收货人信息</h4>
          <p>
            <span>{{ addressData.name }}</span>
            <span>{{ addressData.phone }}</span>
          </p>
          <p>{{ addressData.province }}{{ addressData.city }}{{ addressData.quxian }}{{ addressData.address }}</p>
        </div>
        <div class="arrow">
          <img class="max" src="/static/pingtuan/goods/details_icon_arrows@2x.png" alt="">
        </div>
      </div>
      <div v-else class="no-address tcenter" @click="fnAddress()">
        <div class="icon">
          <img class="max" src="/static/pingtuan/order/order_but_add@2x.png" alt="">
        </div>
        <p>
          您还没有收货地址，点击这里添加
        </p>
      </div>
    </div>
    <div class="goods">
      <div class="head">
        <div class="head-shop">
          <i class="icon-shop"> <img class="max" src="/static/pingtuan/order/order_icon_shop@2x.png"> </i>
          <span>亿养神免费领仪器旗舰店</span>
        </div>
        <div class="head-num">
          {{ querys.count }}件商品
        </div>
      </div>

      <ul class="con" id="shopList">
        <li class="flex-display flex-center-align">
          <img :src="goodsData.share_photo">
          <div class="flex-num-one flex-column-start conInfo">
            <h3>{{ goodsData.ptgoods_name }}</h3>
            <p class="text-overflow-2"></p>
            <div class="flex-row-between">
              <span class="conCart wrap-party__color">￥{{ querys.get_type == 'buy'?goodsSpecActive.price:goodsSpecActive.pt_price }}</span>
              <font class="conNum">x{{ querys.count }}</font>
            </div>
            <div>
              <span class="_standard">规格：{{ specText }}</span>
            </div>
          </div>
        </li>
      </ul>

      <div class="total">
        商品总金额：
        <span class="wrap-party__color">￥{{ totalPrice }}</span>
      </div>
      <div class="postage flex-display flex-center-align">
        邮费
        <span>包邮</span>
      </div>
    </div>

    <footer class="flex-display flex-right-pack" id="footer">
      合计：
      <span id="footTotal">￥{{ totalPrice }}</span>
      <button class="theme-background" @click="fnOpenNewPay">提交订单</button>
    </footer>
  </div>
</template>

<script>
import ComHeader from "@/common/ComHeader";
import api from "@/fetch/api";
import { Toast, Indicator } from "mint-ui";
import {  ptGoodsInfoDetail, joinGroupBuy, joinGroupBuyAlone } from '@/api/pingtuan'
export default {
  components: {
    ComHeader,
    Indicator,
  },
  data() {
    return {
      addressData: null,
      goodsData: null,
      querys: this.$route.query,
      totalPrice: 0,
      specText: null,
      goodsSpecActive: null,
    }
  },
  created() {
    this.getAddress();
    console.log(this)
    this.ptGoodsInfoDetail();
  },
  methods: {
    joinGroupBuyAlone() {
      joinGroupBuyAlone({
        uid: this.getUserInfo().id,
        get_type: this.$route.query.get_type,
        goods_id: this.$route.query.specId,
        pt_code: this.$route.query.pt_code,
        site_id: this.addressData.seqid,
        count: this.$route.query.count
      }).then(res=>{
        console.log(res)
        if(res.code == 0){
          Toast(res.msg)
          this.$router.push({
            path: "/pingtuan/pay",
            query: {
              totalPrice: this.totalPrice,
              orderCode: res.result.order_code[0],
              get_type: this.$route.query.get_type,
            }
          });
        }else{
          Toast(res.msg)
        }
      })
    },
    // 获取拼团商品详情
    ptGoodsInfoDetail() {
      ptGoodsInfoDetail(this.$route.query.goods_id).then(res=>{
        this.print(res.data)
        this.goodsData = res.data;
        var specPrice = '';
        for (var idx in this.goodsData.pt_specification) {
          if(this.goodsData.pt_specification[idx].id == this.querys.specId){
            this.goodsSpecActive = this.goodsData.pt_specification[idx]
            this.specText = this.goodsData.pt_specification[idx].norm_name
            if(this.querys.get_type == 'buy'){
              specPrice = this.goodsData.pt_specification[idx].price
            }else{
              specPrice = this.goodsData.pt_specification[idx].pt_price
            }

          }
        }
        this.totalPrice = (specPrice * this.querys.count).toFixed(2)
      })
    },
    // 创建拼团订单
    joinGroupBuy() {
      joinGroupBuy({
        uid: this.getUserInfo().id,
        get_type: this.$route.query.get_type,
        goods_id: this.$route.query.specId,
        pt_code: this.$route.query.pt_code,
        site_id: this.addressData.seqid,
        count: this.$route.query.count
      }).then(res=>{
        console.log(res)
        if(res.code == 0){
          Toast(res.msg)
          this.$router.push({
            path: "/pingtuan/pay",
            query: {
              totalPrice: this.totalPrice,
              orderCode: res.result.order_code[0],
              get_type: this.$route.query.get_type,
            }
          });
        }else{
          Toast(res.msg)
        }
      })
    },
    // 新增编辑地址
    fnAddress() {
      this.$router.push({
        path: "/address",
        query: {
          source: 'pt'
        }
      });
    },
    // 确定订单
    fnOpenNewPay() {
      // 单独购买
      if(this.$route.query.get_type == 'buy'){
        this.joinGroupBuyAlone();
      // 开团 参团
      }else{
        this.joinGroupBuy();
      }

    },
    // 获取收货地址
    getAddress() {
      Indicator.open({
        text: "加载中...",
        spinnerType: "fading-circle"
      });
      let params = {
        username: this.getUserId()
      };
      api
        .takeaddressList(params)
        .catch(result => {
          console.log(result);
          Indicator.close();
          if (result.status === 200 && result.data.data.length) {
            result.data.data.forEach(item => {
              if (item.isdefault) {
                this.addressData = item;
              }
            });
            if (window.localStorage.getItem("adsData")) {
              this.addressData = JSON.parse(window.localStorage.getItem("adsData"));
            }else{
              this.addressData = this.addressData || result.data.data[0];
            }

          }
        })
        .then(err => {
          Indicator.close();
        });
    },
  }
}
</script>

<style lang="scss" scoped>
.define{
  padding-top: 1.8rem;
  .address{
    background-color: #fff;
    padding: .5rem;
    position: relative;
    &::before{
      content: "";
      display: block;
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      width: 100%;
      height: 2px;
      background: url('/static/pingtuan/order/order_img_adorn@2x.png') center center no-repeat;
      background-size: 100% 100%;
    }
    .has-address{
      position: relative;
      h4{
        font-size: .42rem;
        font-weight: 600;
        padding-bottom: .1rem;
      }
      p{
        padding-bottom: .1rem;
        color: #666;
        font-size: .3rem;
      }
      .arrow{
        position: absolute;
        right: 0rem;
        top: .3rem;
        width: .8rem;
        opacity: .7;
      }
    }
    .no-address{
      text-align: center;
      .icon{
        width: 1.2rem;
        margin: 0 auto;
      }
      p{
        margin-top: .4rem;
        color: #666;
      }
    }
  }
  footer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 49px;
    line-height: 49px;
    background: #fff;
    color: rgba(160, 154, 155, 1);
  }

  footer > button {
    width: 125px;
    height: 100%;
    text-align: center;
    font-size: 14px;
    color: #fff;
    margin-left: 8px;
    outline: none;
    border: none;
  }

  .goods{
    background-color: #fff;
    margin-top: .4rem;
    padding: .3rem;
    .head{
      overflow: hidden;
      font-size: .32rem;
      .head-shop{
        float: left;
        .icon-shop{
          display: inline-block;
          width: .4rem;
        }
      }
      .head-num{
        float: right;
      }
    }
  }

  .con li {
    width: 100%;
    box-sizing: border-box;
    height: auto;
    padding-bottom: 15px;
    position: relative;
    margin-top: .3rem;
    border-top: 1px solid #eee;
    padding-top: .3rem;
  }
  ._standard {
    font-size: 12px;
    height: 30px;
    line-height: 30px;
    background: #e5e5e5;
    padding: 5px;
  }
  .con li img {
    width: 84px;
    height: 84px;
    display: block;
    margin-right: 12px;
  }
  .conInfo {
    height: 84px;
  }

  .conInfo p {
    font-size: 12px;
    line-height: 18px;
  }

  .conSpec {
    margin-top: 7px;
    color: rgba(160, 154, 155, 1);
    font-size: 12px;
    margin-bottom: 9px;
  }

  .conCart {
    font-size: 16px;
    padding: .15rem 0;
  }

  .conNum {
    color: rgba(160, 154, 155, 1);
    font-size: 10px;
  }

  .total {
    width: 100%;
    height: 40px;
    line-height: 40px;
    position: relative;
    font-size: 16px;
    font-weight: bolder;
  }

  .total span {
    font-weight: 400;
    position: absolute;
    right: 0px;
  }
  .postage {
    width: 100%;
    height: 40px;
    line-height: 40px;
    font-weight: bolder;
    position: relative;
    font-size: 12px;
  }

  .postage span {
    position: absolute;
    right: 0px;
  }

}
</style>
